//
//  GQHTransactionController.m
//  Xanthium
//
//  Created by GuanQinghao on 28/02/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHTransactionView.h"

#pragma mark Controller
#import "GQHTransactionController.h"
#import "GQHTransactionDetailController.h"
#import "GQHTransactionInBankController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHTransactionController () <GQHTransactionViewDelegate> {
    
#pragma mark Data
    NSMutableArray *contentArray;
#pragma mark View
    /// 自定义根视图
    GQHTransactionView *rootView;
}

@end


@implementation GQHTransactionController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHTransactionView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    [self fetchServiceData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"我的转账";
    [self.qh_rightButton setTitle:@"添加" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [rootView.qh_tableView.mj_header beginRefreshing];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    GQHTransactionDetailController *detailController = [[GQHTransactionDetailController alloc] init];
    detailController.hidesBottomBarWhenPushed = YES;
    detailController.qh_editable = NO;
    detailController.qh_contentDictionary = contentArray[indexPath.row];
    [self.navigationController pushViewController:detailController animated:YES];
}
#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    
    GQHTransactionInBankController *inBankController = [[GQHTransactionInBankController alloc] init];
    inBankController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:inBankController animated:YES];
}


#pragma mark --PrivateMethod
- (void)fetchServiceData {
    
    /// 获取服务器数据 上拉加载下拉刷新
    __block int page = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"userId"];
    [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
    [parameters setObject:ROWS forKey:@"rows"];
    
    rootView.qh_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        /// 刷新转账记录
        page = 1;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buOfflineRechargeRecord/1/list"];
        [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
            DLog(@"%@-%@",message,data);
            
            if (0 == [code intValue]) {
                
                contentArray = [NSMutableArray arrayWithArray:[data objectForKey:@"list"]];
                rootView.qh_contentArray = contentArray;
                [rootView.qh_tableView reloadData];
                [rootView.qh_tableView.mj_header endRefreshing];
            } else {
                
                [self showToastWithText:message];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self qh_alertWithTitle:nil message:errorString actionTitle:nil handler:nil completion:nil];
        }];
    }];
    
    rootView.qh_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        /// 加载更多
        page++;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        NSString *URLString = [[NSString qh_APIURLString] stringByAppendingString:@"/buOfflineRechargeRecord/1/list"];
        [GQHNetHelper qh_requestGET:URLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
            
            if (0 == [code intValue]) {
                
                NSArray *responseDataArray = [data objectForKey:@"list"];
                
                if (responseDataArray && responseDataArray.count > 0) {
                    
                    [contentArray addObjectsFromArray:responseDataArray];
                    rootView.qh_contentArray = contentArray;
                    [rootView.qh_tableView reloadData];
                    [rootView.qh_tableView.mj_footer endRefreshing];
                } else {
                    
                    [rootView.qh_tableView.mj_footer endRefreshingWithNoMoreData];
                }
            } else {
                
                [self showToastWithText:message];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self qh_alertWithTitle:nil message:errorString actionTitle:nil handler:nil completion:nil];
        }];
    }];
}
#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
